package com.foxzzz.binary.search;

/**
 * @author: [wujie]
 * @createTime: [2019/7/4 12:11]
 * @description:
 */
public class Solution744 {
    public char nextGreatestLetter(char[] letters, char target) {
        int low = 0;
        int high = letters.length;
        int mid = 0;
        while (low <= high) {
            mid = (low + high) / 2;
            if (mid == letters.length || target == letters[mid]) {
                break;
            } else if (target < letters[mid]) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        if (mid == letters.length) {
            mid = 0;
        }
        else {
            while(letters[mid] == target||(letters[mid]<target&&letters[mid+1]>target)){
                mid=(mid+1)%letters.length;
            }
        }
        return letters[mid];
    }

    public static void main(String[] args) {
    /*    System.out.println(new Solution744().nextGreatestLetter(new char[]{'c', 'f', 'j'}, 'a'));
        System.out.println(new Solution744().nextGreatestLetter(new char[]{'c', 'f', 'j'}, 'f'));
        System.out.println(new Solution744().nextGreatestLetter(new char[]{'c', 'f', 'j'}, 'z'));
        System.out.println(new Solution744().nextGreatestLetter(new char[]{'c', 'f', 'j'}, 'j'));
        System.out.println(new Solution744().nextGreatestLetter(new char[]{'c', 'f', 'j'}, 'c'));*/
        System.out.println(new Solution744().nextGreatestLetter(new char[]{'c', 'f', 'j'}, 'd'));
        System.out.println(new Solution744().nextGreatestLetter(new char[]{'e', 'e', 'e','n','n','n'}, 'n'));

    }
}
